package cn.bincker.mybatis.encrypt.data.migration.utils;

public class NameUtils {
    public static String entityClassToTableName(Class<?> cls){
        return camelToSnakeCase(cls.getSimpleName());
    }

    public static String camelToSnakeCase(String name){
        var result = new StringBuilder();
        char[] charArray = name.toCharArray();
        for (int i = 0; i < charArray.length; i++) {
            char c = charArray[i];
            if (Character.isUpperCase(c)) {
                if (i != 0) result.append('_');
                result.append(Character.toLowerCase(c));
            }else{
                result.append(c);
            }
        }
        return result.toString();
    }

    public static String snakeCaseToCamel(String name){
        var charArray = name.toCharArray();
        var result = new StringBuilder();
        var nextNeedUpperCase = false;
        for (char c : charArray) {
            if (c == '_') {
                nextNeedUpperCase = true;
            } else if (nextNeedUpperCase) {
                result.append(Character.toUpperCase(c));
                nextNeedUpperCase = false;
            } else {
                result.append(Character.toLowerCase(c));
            }
        }
        return result.toString();
    }
}
